﻿
/* 
 * Since 2008 - 2012
 */
package com.hh.bss.project.controller;

import static com.hh.bss.util.extjs.JsonHelper.outJson;
import static com.hh.bss.util.extjs.JsonHelper.outJsonArray;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.hh.bss.auth.client.OnlineManager;
import com.hh.bss.auth.client.OnlineUser;
import com.hh.bss.base.BaseSpringController;
import com.hh.bss.common.page.Page;
import com.hh.bss.common.page.PageRequest;
import com.hh.bss.project.entity.ContractJob;
import com.hh.bss.project.entity.ContractJobCvRecom;
import com.hh.bss.project.service.ContractJobService;
import com.hh.bss.project.vo.query.ContractJobQuery;
import com.hh.bss.util.extjs.ExtJsPageHelper;
import com.hh.bss.util.extjs.ListRange;


@Controller
public class ContractJobController extends BaseSpringController{
	//默认多列排序,example: username desc,createTime asc
	protected static final String DEFAULT_SORT_COLUMNS = null; 
	
	private ContractJobService contractJobService;
	
	private ContractJob contractJob;
	java.lang.Integer id = null;
	private String[] items;

	
	
	/** 通过spring自动注入 */
	public void setContractJobServiceImpl(ContractJobService service) {
		this.contractJobService = service;
	}	
	
	
	
	public void setId(java.lang.Integer val) {
		this.id = val;
	}

	public void setItems(String[] items) {
		this.items = items;
	}



	/**
	 * ExtGrid使用
	 * 列表
	 * @throws IOException
	 */
	public void list(HttpServletRequest request,HttpServletResponse response,ContractJobQuery contractJobQuery) throws IOException
	{
		OnlineManager onlineManager=super.getOnlineManager(request, response);
		if (onlineManager!=null){
			OnlineUser onlineUser=onlineManager.getOnlineUser();
			contractJobQuery.setOnlineUser(onlineUser);
		} 
		PageRequest<Map> pr = ExtJsPageHelper.createPageRequestForExtJs(request, DEFAULT_SORT_COLUMNS);
		Page page = contractJobService.findPage(contractJobQuery);
		
		List<ContractJob> ContractJoblist = (List) page.getResult();
		ListRange<ContractJob> resultList = new ListRange<ContractJob>();
		resultList.setRows(ContractJoblist);
		resultList.setTotal(page.getTotalCount());
		resultList.setMessage("ok");
		resultList.setSuccess(true);
		outJson(response,resultList);
	}
	public String managerPage(HttpServletRequest request, HttpServletResponse response) throws IOException {	
		return "forward:/jsp/jobManagerPage.jsp";	
	}
	public String customerJob(HttpServletRequest request, HttpServletResponse response) throws IOException {	
		return "forward:/jsp/project/contractJob/customerJobPage.jsp";	
	}
	public String info(HttpServletRequest request, HttpServletResponse response) throws IOException {	
		return "forward:/jsp/project/contractJob/jobDetail.jsp";	
	}
	public String selectJobPage(HttpServletRequest request, HttpServletResponse response) throws IOException {	
		return "forward:/jsp/project/contractJob/selectJobPage.jsp";	
	}
	public void joblist(HttpServletRequest request,HttpServletResponse response,ContractJobQuery contractJobQuery) throws IOException
	{
		
		PageRequest<Map> pr = ExtJsPageHelper.createPageRequestForExtJs(request, DEFAULT_SORT_COLUMNS);
		OnlineManager onlineManager=super.getOnlineManager(request, response);
		if (onlineManager!=null){
			OnlineUser onlineUser=onlineManager.getOnlineUser();
			contractJobQuery.setOnlineUser(onlineUser);
		}
		Page page = contractJobService.findJobPage(contractJobQuery);
		
		List<ContractJob> ContractJoblist = (List) page.getResult();
		ListRange<ContractJob> resultList = new ListRange<ContractJob>();
		resultList.setRows(ContractJoblist);
		
		resultList.setTotal(page.getTotalCount());
		resultList.setMessage("ok");
		resultList.setSuccess(true);
		outJson(response,resultList);
	}
	public void jobproject(HttpServletRequest request,HttpServletResponse response,ContractJobQuery contractJobQuery) throws IOException
	{
		PageRequest<Map> pr = ExtJsPageHelper.createPageRequestForExtJs(request, DEFAULT_SORT_COLUMNS);
		OnlineManager onlineManager=super.getOnlineManager(request, response);
		if (onlineManager!=null){
			OnlineUser onlineUser=onlineManager.getOnlineUser();
			contractJobQuery.setOnlineUser(onlineUser);
		}
		Page page = contractJobService.findProjectJobPage(contractJobQuery);
		
		List<ContractJob> ContractJoblist = (List) page.getResult();
		ListRange<ContractJob> resultList = new ListRange<ContractJob>();
		resultList.setRows(ContractJoblist);
		
		resultList.setTotal(page.getTotalCount());
		resultList.setMessage("ok");
		resultList.setSuccess(true);
		outJson(response,resultList);
	}
	
	public void jobinproject(HttpServletRequest request,HttpServletResponse response,ContractJobQuery contractJobQuery) throws IOException
	{
		PageRequest<Map> pr = ExtJsPageHelper.createPageRequestForExtJs(request, DEFAULT_SORT_COLUMNS);
		OnlineManager onlineManager=super.getOnlineManager(request, response);
		if (onlineManager!=null){
			OnlineUser onlineUser=onlineManager.getOnlineUser();
			contractJobQuery.setOnlineUser(onlineUser);
		}
		Page page = contractJobService.findJobInProjectPage(contractJobQuery);
		
		List<ContractJob> ContractJoblist = (List) page.getResult();
		ListRange<ContractJob> resultList = new ListRange<ContractJob>();
		resultList.setRows(ContractJoblist);
		
		resultList.setTotal(page.getTotalCount());
		resultList.setMessage("ok");
		resultList.setSuccess(true);
		outJson(response,resultList);
	}
	public void projectjoblist(HttpServletRequest request,HttpServletResponse response,ContractJobQuery contractJobQuery) throws IOException
	{
		OnlineManager onlineManager=super.getOnlineManager(request, response);
		if (onlineManager!=null){
			OnlineUser onlineUser=onlineManager.getOnlineUser();
			contractJobQuery.setOnlineUser(onlineUser);
		}
		
        List companyDeptTitlelist = (List)contractJobService.findProjectJob(contractJobQuery);

        outJsonArray(response,companyDeptTitlelist);
	}
	
	public void get(HttpServletRequest request,HttpServletResponse response,ContractJob contractJob) throws IOException
	{
		Map<String, Object> result = new HashMap<String, Object>();
		try
		{
			ContractJob oldContractJob = (ContractJob)contractJobService.getById(contractJob.getId());
                         
			
            //取招聘岗位
			
			if(oldContractJob!=null) result.put("data", oldContractJob);
			result.put("success", true);
			result.put("msg", "成功!");
		}
		catch (Exception e)
		{
			result.put("failure", true);
			result.put("msg", e.getMessage());
			e.printStackTrace();
		}
		outJson(response,result);
	}
	public void getsimple(HttpServletRequest request,HttpServletResponse response,ContractJob contractJob) throws IOException
	{
		Map<String, Object> result = new HashMap<String, Object>();
		try
		{
			ContractJob oldContractJob = (ContractJob)contractJobService.getById(contractJob.getId());
                         
			
            //取招聘岗位
			Map m=new HashMap();
			if(oldContractJob!=null){
				m.put("id", oldContractJob.getId());
				m.put("name", oldContractJob.getName());
				m.put("servicesCharge", oldContractJob.getServicesCharge());
				
				result.put("data", m);
			}
			result.put("success", true);
			result.put("msg", "成功!");
		}
		catch (Exception e)
		{
			result.put("failure", true);
			result.put("msg", e.getMessage());
			e.printStackTrace();
		}
		outJson(response,result);
	}

	/**
	 * extGrid保存
	 * @throws IOException
	 */
	@SuppressWarnings("unchecked")
	public void save(HttpServletRequest request,HttpServletResponse response,ContractJob contractJob) throws IOException
	{
		Map<String, Object> result = new HashMap<String, Object>();
		try
		{
                        //设置创建用户和创建时间
			com.hh.bss.auth.client.OnlineManager onlineManager=super.getOnlineManager(request, response);
			com.hh.bss.auth.client.OnlineUser onlineUser =null;
			if (onlineManager!=null){
					 onlineUser=onlineManager.getOnlineUser();
					if (onlineUser!=null&&com.hh.bss.util.BeanUtils.isExistsProperty(ContractJob.class,"creator")) {
						try{com.hh.bss.util.BeanUtils.setProperty(contractJob,"creator",onlineUser.getUsername());}catch(Exception ex){}
					}
				}
							
			if (com.hh.bss.util.BeanUtils.isExistsProperty(ContractJob.class,"createDt")) {
					try{com.hh.bss.util.BeanUtils.setProperty(contractJob,"createDt",new Date());}catch(Exception ex){}
				}
			MultipartHttpServletRequest multipartRequest=null;
			MultipartFile multipartFile =null;
			
		       try{
		    	   multipartRequest = (MultipartHttpServletRequest) request;
		    	   multipartFile = multipartRequest.getFile("uploadFile");		    	  
		       }catch(Exception ex){ex.printStackTrace();}
		        
		       boolean autoGen=true;
		       if (multipartRequest!=null&&multipartFile!=null&&!multipartFile.isEmpty()){
		    	   //contractJobCvRecomService.saveFile(multipartRequest, multipartRequest, contractJobCvRecom);
		    	   contractJobService.saveFile(multipartRequest, multipartFile, contractJob);
		    	   autoGen=false;
		       }
		    // System.out.println("============="+contractJob.getCreator()+"=============");
			//if (!StringHelper.isEmpty(contractJob.getOpenDString())) searchProjectCvCrm.setPlanDt(DateHelper.toDate(searchProjectCvCrm.getPlanDtStr(), DateHelper.DEFAULT_DATETIME_FORMAT));
			if (contractJob.getStatus()==null) contractJob.setStatus(0);
			String jobNo = contractJobService.getNextPositionNo(contractJob);//new PositionUtils().getNextPositionNo(contractJob.getCreator());
			// System.out.println("**********"+jobNo+"**********");
			contractJob.setJobNo(jobNo);
			
			contractJobService.save(contractJob);
			
			if (autoGen){
				//取消自动生成
		    	   contractJobService.genJDDocCn(multipartRequest,onlineUser, contractJob);
		    	   contractJobService.getEntityDao().getHibernateTemplate().update(contractJob);
		    	   contractJobService.getEntityDao().flush();
		    	   
			 }
			result.put("success", true);
			result.put("msg", "添加成功!");
		}
		catch (Exception e)
		{
			result.put("failure", true);
			result.put("msg", e.getMessage());
			e.printStackTrace();
		}
		outJson(response,result);
	}
	public void autogen(HttpServletRequest request,HttpServletResponse response,ContractJob contractJob) throws IOException
	{
		Map<String, Object> result = new HashMap<String, Object>();
		try
		{
			ContractJob oldContractJob = (ContractJob)contractJobService.getById(contractJob.getId());
                         
			String type=request.getParameter("type"); //type=1，中文,type=2英文，默认是中文
                        //设置修改用户和修改时间 
                        com.hh.bss.auth.client.OnlineManager onlineManager=super.getOnlineManager(request, response);
                        com.hh.bss.auth.client.OnlineUser onlineUser=null;
			if (onlineManager!=null){
					 onlineUser=onlineManager.getOnlineUser();
					if (onlineUser!=null&&com.hh.bss.util.BeanUtils.isExistsProperty(ContractJobCvRecom.class,"modifier")) {
						try{com.hh.bss.util.BeanUtils.setProperty(oldContractJob,"modifier",onlineUser.getUsername());}catch(Exception ex){}
					}
				}
							
			if (com.hh.bss.util.BeanUtils.isExistsProperty(ContractJobCvRecom.class,"modifiedDt")) {
					try{com.hh.bss.util.BeanUtils.setProperty(oldContractJob,"modifiedDt",new Date());}catch(Exception ex){}
				}
			if ("2".equals(type))
			    contractJobService.genJDDocEn(request,onlineUser,oldContractJob);
			else
				contractJobService.genJDDocCn(request,onlineUser,oldContractJob);
			   

			 contractJobService.update(oldContractJob);
			result.put("success", true);
			result.put("file", oldContractJob.getJobFile());
			result.put("msg", "生成成功!");
		}
		catch (Exception e)
		{
			result.put("failure", true);
			result.put("msg", e.getMessage());
			e.printStackTrace();
		}
		outJson(response,result);
	}
	/**
	 * extGrid修改
	 * @throws IOException
	 */
	public void update(HttpServletRequest request,HttpServletResponse response,ContractJob contractJob) throws IOException
	{
		Map<String, Object> result = new HashMap<String, Object>();
		try
		{
                        ContractJob oldcontractJob = (ContractJob)contractJobService.getById(contractJob.getId());
                         
			bind(request,oldcontractJob);
                        //设置修改用户和修改时间 
                        com.hh.bss.auth.client.OnlineManager onlineManager=super.getOnlineManager(request, response);
			if (onlineManager!=null){
					com.hh.bss.auth.client.OnlineUser onlineUser=onlineManager.getOnlineUser();
					if (onlineUser!=null&&com.hh.bss.util.BeanUtils.isExistsProperty(ContractJob.class,"modifier")) {
						try{com.hh.bss.util.BeanUtils.setProperty(oldcontractJob,"modifier",onlineUser.getUsername());}catch(Exception ex){}
					}
				}
							
			if (com.hh.bss.util.BeanUtils.isExistsProperty(ContractJob.class,"modifiedDt")) {
					try{com.hh.bss.util.BeanUtils.setProperty(oldcontractJob,"modifiedDt",new Date());}catch(Exception ex){}
				}

			MultipartHttpServletRequest multipartRequest=null;
			MultipartFile multipartFile =null;
			
		       try{
		    	   multipartRequest = (MultipartHttpServletRequest) request;
		    	   multipartFile = multipartRequest.getFile("uploadFile");		    	  
		       }catch(Exception ex){ex.printStackTrace();}
		        

		       if (multipartRequest!=null&&multipartFile!=null&&!multipartFile.isEmpty()){
		    	   contractJobService.saveFile(multipartRequest, multipartFile, oldcontractJob);

		       }

			contractJobService.update(oldcontractJob);
			result.put("success", true);
			if(contractJobService.hasOfferBill(oldcontractJob.getId())){
				result.put("msg", "修改成功!职位下有账单,此次的职位负责人与合作人,业务助理的修改不会同步到该账单关联的录用offer分配中.");
			}else{
				result.put("msg", "修改成功!");
			}
			
			
		}
		catch (Exception e)
		{
			result.put("failure", true);
			result.put("msg", e.getMessage());
			e.printStackTrace();
		}
		outJson(response,result);
	}
	
	/**
	 * extGrid删除
	 * @throws IOException
	 */
	public void delete(HttpServletRequest request,HttpServletResponse response) throws IOException
	{
		String ids = request.getParameter("ids");
		String[] idarray = ids.split(",");
		Map<String, Object> result = new HashMap<String, Object>();
		try
		{
			for (int i = 0; i < idarray.length; i++)
			{
				java.lang.Integer id = new java.lang.Integer((String)idarray[i]);
				contractJobService.removeById(id);
			}
			result.put("success", true);
			result.put("msg", "删除成功");
		}
		catch (Exception e)
		{
			result.put("failure", true);
			result.put("msg", e.getMessage());
			e.printStackTrace();
		}
		outJson(response,result);
	}
	/**
	 * 
	 * 对官网(www.tbcg.cn) 发布职位列表
	 * 
	 * 
	 */
	public void tbcglist(HttpServletRequest request,HttpServletResponse response,ContractJobQuery contractJobQuery) throws IOException{
		
		PageRequest<Map> pr = ExtJsPageHelper.createPageRequestForExtJs(request, DEFAULT_SORT_COLUMNS);
		OnlineManager onlineManager=super.getOnlineManager(request, response);
		if (onlineManager!=null){
			OnlineUser onlineUser=onlineManager.getOnlineUser();
			contractJobQuery.setOnlineUser(onlineUser);
		}
		Page page = contractJobService.findAuditJobPage(contractJobQuery);
		
		List<ContractJob> ContractJoblist = (List) page.getResult();
		ListRange<ContractJob> resultList = new ListRange<ContractJob>();
		resultList.setRows(ContractJoblist);
		
		resultList.setTotal(page.getTotalCount());
		resultList.setMessage("ok");
		resultList.setSuccess(true);
		outJson(response,resultList);
	}
	
	public void passedaudit(HttpServletRequest request,HttpServletResponse response){
		Map<String, Object> result = new HashMap<String, Object>();
		String ids = request.getParameter("ids");
		OnlineManager onlineManager=super.getOnlineManager(request, response);
		if(onlineManager !=null){
			OnlineUser onlineUser=onlineManager.getOnlineUser();
			String auditor = onlineUser.getUsername();
			try {
				if(ids != null && ids.length() > 0 ){
					String [] idArr = ids.split(",");
					for(String id : idArr){
						int num = contractJobService.passedAudit(id, auditor);
					}
				}
			} catch (Exception e) {
				result.put("failure", true);
				result.put("msg", e.getMessage());
				e.printStackTrace();
				
			}
			result.put("success", true);
			result.put("msg", "审核成功!");
			outJson(response,result);
			
		}
	}
	
	public void rejectaudit(HttpServletRequest request,HttpServletResponse response){
		Map<String, Object> result = new HashMap<String, Object>();
		String ids = request.getParameter("ids");
		OnlineManager onlineManager=super.getOnlineManager(request, response);
		if(onlineManager !=null){
			OnlineUser onlineUser=onlineManager.getOnlineUser();
			String auditor = onlineUser.getUsername();
			try {
				if(ids != null && ids.length() > 0 ){
					String [] idArr = ids.split(",");
					for(String id : idArr){
						int num = contractJobService.rejectAudit(id, auditor);
					}
				}
			} catch (Exception e) {
				result.put("failure", true);
				result.put("msg", e.getMessage());
				e.printStackTrace();
				
			}
			result.put("success", true);
			result.put("msg", "拒绝审核成功!");
			outJson(response,result);
			
		}
	}
}
